package com.fzkj.python;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/** java调用python. */
public class JavaPy {

    public static String py(String arguments[]){
        String result = "";
        try {
            Process pro = Runtime.getRuntime().exec(arguments);
            BufferedReader reader = new BufferedReader(new InputStreamReader(pro.getInputStream(), "GBK"));
            String line = "";
            while ((line = reader.readLine()) != null) {
                result = line;
            }
            reader.close();
            //java代码中的process.waitFor()返回值为0表示调用python脚本成功，
            //返回值为1表示调用python脚本失败，这和通常意义上见到的0与1定义正好相反
            int re = pro.waitFor();
            //System.out.println(re);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
        String arguments[] = new String[] {"python", "E:/img.py", "E:/py_pic/"};
        String py = py(arguments);
        System.out.println(py);
    }

}
